package com.hatricks.is.gld.work.transform;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @packageName: com.hatricks.transform
 * @className: ModelFresh
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2024-06-14 19:41
 * @description: 模型刷新
 */
@SuppressWarnings("VulnerableCodeUsages")
@Slf4j
public class ModelFresh {

    public static void main(String[] args) {
        File file = FileUtil.file("C:\\work_space\\java_work_space\\public_utils\\src\\main\\java\\com\\hatricks\\transform\\tt.txt");
        String data = IoUtil.readUtf8(FileUtil.getInputStream(file));
        JSONArray jsonArray = JSONObject.parseArray(data);
        List<JSONObject> result = ListUtil.toLinkedList();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            JSONObject jo = new JSONObject();
            jo.put("mongodbId", json.getString("mongodb_id") + "a");
            String enterpriseId = json.getString("enterprise_id");
            jo.put("enterpriseId", StrUtil.sub(enterpriseId, 0, enterpriseId.length() - 1));
            jo.put("productSource", json.getString("product_source"));
            result.add(jo);
        }

        Map<String, Map<String, List<String>>> ids = result.stream().collect(Collectors.groupingBy(json ->
                        json.getString("enterpriseId"), LinkedHashMap::new,
                Collectors.groupingBy(json -> json.getString("productSource"),
                        Collectors.mapping(zz -> zz.getString("mongodbId"), Collectors.toList()))));
        //System.out.println(ids.keySet().size());

        // 进行企业id过滤后进行数据刷新
        List<String> conditions = ListUtil.toList("6964736666988785807", "7029732189857296461", "7178650894170165647", "6526031142599152492");
        if (CollUtil.isNotEmpty(conditions)) {
            String token = "cn-ee759f2c-93df-40c0-82eb-36aa22a926d6-ut";
            for (Map.Entry<String, Map<String, List<String>>> node : ids.entrySet()) {
                if (CollUtil.contains(conditions, node.getKey())) {
                    Map<String, List<String>> psMapping = node.getValue();
                    log.info("当前的企业id为:[{}]", node.getKey());
                    for (Map.Entry<String, List<String>> psNode : psMapping.entrySet()) {
                        log.info("来源为:[{}] 刷新的工程id为:[{}]", psNode.getKey(), psNode.getValue());
                        JSONObject jsonBody = new JSONObject();
                        jsonBody.put("ids", psNode.getValue());
                        jsonBody.put("productSource", psNode.getKey());
                        jsonBody.put("enterpriseId", node.getKey());
                        jsonBody.put("token", token);
                        String body = HttpUtil.post("https://dcost-sub-test-sprint.glodon.com/api/service/archive/bizData/refreshHistory", jsonBody.toJSONString());
                        log.info("返回的数据为:[{}]", body);
                        ThreadUtil.safeSleep(1000);
                    }
                }
            }
        }
        //System.out.println(JSONArray.toJSONString(ids));
    }

}
